
// const API = 'http://192.168.0.113:7076/';
// import  './ican-H5Api.js';
const authList = require('./authList.js').default.pages;
let API;

/**
 * 获取token
 */
function getToken() {
	return new Promise(function(resolve, reject) {
		uni.login({
			success: res => {

				// 发送 res.code 到后台换取 openId, sessionKey, unionId
				if (res.code) {

					uni.request({
						url: API + "common/xcx/auth",
						data: {
							code: res.code
						},
						header: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						method: 'POST',
						success: res => {
							resolve(res);
						}
					})
				} else {
					console.log('获取用户登录态失败！' + res.errMsg);
					reject('error');
				}
			}
		})
	})
}

/**
 * 用户发起接口请求前判断token是否存在
 * 不存在，则请求login接口
 * 存在，直接请求相应接口
 */
function ajax(options) {
	API = getApp() && getApp().globalData ? getApp().globalData.api : '';
	if(API == '') {
		setTimeout(() => {
			ajax(options);
		},500)
	}
	// API = 'http://192.168.0.102:7070/';
	// API = 'http://portalapi.ccchongya.com/';
	if (options.loading) {
		uni.showLoading({
			title: options.loadingText ? options.loadingText : '加载中',
			mask: true
		})
	}
	// #ifdef APP-PLUS
		options.data.applicationType = 'app';
	// #endif
	// #ifdef MP-WEIXIN
	options.data.applicationType = 'xcx';
	// #endif
	// #ifdef H5
	options.data.applicationType = 'gzh';
	// #endif
	//options.data.applicationType = 'xcx';//value = "fiveXcx:五代小程序,xcx:七代小程序,gzh：公众号,app:APP"
	//options.data.brandId = options.data.brandId ? options.data.brandId : uni.getStorageSync("style") ? uni.getStorageSync("style").brandId : '';
	uni.request({
		url: API + options.url,
		data: options.data,
		method: options.method ? options.method : 'POST',
		header: {
			'Content-Type': 'application/x-www-form-urlencoded',
			'Authorization': uni.getStorageSync("token") ? uni.getStorageSync("token") : '',
			'lang': uni.getStorageSync('locale_key') ? uni.getStorageSync('locale_key') : '',
		},
		success: (response) => {
			if (response.data.code != 501 && response.data.code != 200 && response.data.code != 300 && response.data.code != 401 && !options.complete) {
				console.log(response.data)
				if(options.loading){
					setTimeout(() => {
						uni.showToast({
							title: response.data.message,
							icon: 'none',
							duration: 2000
						})
					},200)
				}else{
					uni.showToast({
						title: response.data.message,
						icon: 'none',
						duration: 2000
					})
				}
			}else if (response.data.code == 501) {
				uni.showModal({
					title: '',
					content: response.data.message,
					showCancel: false,
					confirmText: '确认',
					confirmColor: '#DA2A1C',
					success(res) {
						if (res.confirm) {
							if(response.data.message.indexOf('关注') >= 0) {
								go('/pages/user/gzh');
							}
						} else if (res.cancel) {}
					}
				})
			} else if (response.data.code == 401 && !options.isAuth) {
				uni.removeStorageSync('token');
				setTimeout(() => {
					go('/pages/user/passport/login');
				},600)

			} else if (response.data.code == 300) {
				uni.showToast({
					title: '请进行绑定手机',
					icon: 'none',
					duration: 2000
				});
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/wxAuth/bindphone'
					})
				}, 1000);
			} else if (response.data.code == 200) {
				if (options.success) {
					options.success(response.data);
				}
			}

		},
		fail: (response) => {
			if (options.error) {
				options.error(response.data);
			}
		},
		complete: (response) => {
			if (options.loading) {
				uni.hideLoading();
			}
			if (options.complete) {
				options.complete(response.data);
			}
		}
	})
}
let isPay = false;


// function testWxPay() {
// 				const data = {
// 					joinType: **,
// 					name: "***",
// 					totalAmount: *
// 				}
//                // 这个是后端给我的接口，我统一写在了管理api的js文件中
// 				wxPay(data).then(res => {
// 					uni.hideLoading()
// 					console.log(res, "测试微信支付的res")
// 					this.wxParams = res.info.responseData.app_response
// 					//订单对象，从服务器获取
// 					var orderInfos = {
// 						"appid": this.wxParams.appid, // 应用ID（AppID）
// 						"partnerid": this.wxParams.partnerid, // 商户号（PartnerID）
// 						"prepayid": this.wxParams.prepayid, // 预支付交易会话ID
// 						"package": this.wxParams.package, // 固定值
// 						"noncestr": this.wxParams.noncestr, // 随机字符串
// 						"timestamp": this.wxParams.timestamp, // 时间戳（单位：秒）
// 						"sign": this.wxParams.sign, // 签名，这里用的 MD5 签名
// 					};
// 					console.log(this.wxParams, "this.wxParams")
// 					console.log(orderInfos, "orderInfos")
//                   // uniapp官方的Api，可直接到官网搜索查询
// 					uni.requestPayment({
// 						provider: "wxpay",
// 						orderInfo: orderInfos,
// 						success(res) {
// 							console.log('success:' + JSON.stringify(res));
// 							console.log("微信支付成功");
// 						},
// 						fail(err) {
// 							console.log('fail:' + JSON.stringify(err));
// 							console.log("微信支付失败");
// 						}
// 					});
// 				}).catch(err => {
// 					uni.hideLoading()
// 					console.log(err, "测试微信支付err")
// 				})

// }

// function wxPay(options) {
// 		console.log('options', options);
// 	if (isPay) {
// 		return
// 	};
// 	isPay = true;
// 		console.log('1111');
// 	setTimeout(() => {
// 		isPay = false;
// 	},1000)
// 	let param;
// 	if(options.payType == 'alipay') {
// 		param = options.data;
// 	}else{
// 				console.log('2222');
// 		// #ifndef APP-PLUS
// 		param = JSON.parse(options.data);
// 		// #endif
// 		// #ifdef APP-PLUS
// 				console.log('1111222');
// 		// param = options.data;
// 		var typeStr = typeof(options.data)
// 		if (typeStr == 'object'){
// 				param = options.data;
// 		} else if(typeStr == 'string'){
// 				param = JSON.parse(options.data);
// 		}


// 			console.log('111122233333');
// 		param.timestamp = Number(param.timestamp);
// 		// #endif
// 	}
// 			console.log('33333');
// 	console.log('options',options)

// 	// #ifdef H5
// 	if(options.payType == 'alipay') {
// 	window.location.href = r.data.config.config;
// 	}
// 	// #endif



// 	if(options.payType == 'alipay') {
// 			console.log('err', 0000000000000000);
// 			uni.requestPayment({
// 				provider: options.payType,
// 				orderInfo: param,
// 				// #ifdef MP-WEIXIN
// 				timeStamp: param.timeStamp,
// 				nonceStr: param.nonceStr,
// 				package: param.package,
// 				signType: 'MD5',
// 				paySign: param.paySign,
// 				// #endif
// 				success(res) {
// 					if (options.success) {
// 						options.success(res.data);
// 					}
// 				},
// 				fail(res) {
// 					console.log(res);
// 					if (res.errMsg == 'requestPayment:fail cancel') {

// 					} else {
// 						uni.showToast({
// 							title: res.errMsg,
// 							icon: 'none',
// 							duration: 2000
// 						})
// 					}

// 					if (options.error) {
// 						options.error(res.data);
// 					}
// 				}
// 			})
// 	}else{
// 		// 			console.log('err', 11111111111111111111);
// 		// uni.requestPayment({
// 		// 	provider: 'wxpay', // 微信支付
// 		// 	orderInfo: JSON.stringify(r.data.config), //微信订单数据 r.data.config
// 		// 	success: function() {
// 		// 		mHelper.toast('支付成功');
// 		// 		mRouter.redirectTo({ route });
// 		// 	},
// 		// 	fail: function(err) {
// 		// 		console.log('err', err);
// 		// 		mHelper.toast('支付已取消');1
// 		// 	}
// 		// });

// 		// console.log("param",param)

// 		// let wxParams = JSON.parse(param.message)
// 		// console.log("wxParams",wxParams)
// 		// //订单对象，从服务器获取s
// 		// var orderInfos = {
// 		// "appid": wxParams.appid, // 应用ID（AppID）
// 		// "partnerid": wxParams.partnerid, // 商户号（PartnerID）
// 		// "prepayid": wxParams.prepayid, // 预支付交易会话ID
// 		// "package": wxParams.package, // 固定值
// 		// "noncestr": wxParams.noncestr, // 随机字符串
// 		// "timestamp": wxParams.timestamp, // 时间戳（单位：秒）
// 		// "sign": wxParams.sign, // 签名，这里用的 MD5 签名
// 		// };
// 		// console.log("orderInfos",orderInfos)
// 		// console.log("JSON.stringify(param.message)",JSON.stringify(param.message))
// 		uni.requestPayment({
// 			provider: options.payType,
// 			orderInfo: param.message,
// 			success(res) {
// 				if (options.success) {
// 					options.success(res.data);
// 				}
// 			},
// 			fail(res) {
// 		// 			console.log("failfailfail");

// 		// 		console.log(res);
// 		// 		if (res.errMsg == 'requestPayment:fail cancel') {

// 		// 		} else {
// 		// 			uni.showToast({
// 		// 				// title: res.errMsg,
// 		// 				title:"支付已取消",
// 		// 				icon: 'none',
// 		// 				duration: 2000
// 		// 			})
// 		// 		}
// 				uni.showToast({
// 					// title: res.errMsg,
// 					title:"支付已取消",
// 					icon: 'none',
// 					duration: 2000
// 				})

// 				if (options.error) {
// 					options.error(res.data);
// 				}
// 			}
// 		})

// 	}



// }
/**
	 * base64格式转为文件流
	 * @param base64 base64地址
	 * @returns {File}
	 */
 function	base64ToFile(base64) {
		return new Promise(function(resolve, reject) {
			if (typeof window === 'object' && 'document' in window) {
				base64 = base64.split(',')
				var type = base64[0].match(/:(.*?);/)[1]
				var str = atob(base64[1])
				var n = str.length
				var array = new Uint8Array(n)
				while (n--) {
					array[n] = str.charCodeAt(n)
				}
				return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], {
					type: type
				})))
			}
			var extName = base64.match(/data\:\S+\/(\S+);/)
			if (extName) {
				extName = extName[1]
			} else {
				reject(new Error('base64 error'))
			}
			var fileName = Date.now() + '.' + extName
			if (typeof plus === 'object') {
				var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
				bitmap.loadBase64Data(base64, function() {
					var filePath = '_doc/uniapp_temp/' + fileName
					bitmap.save(filePath, {}, function() {
						bitmap.clear()
						resolve(filePath)
					}, function(error) {
						bitmap.clear()
						reject(error)
					})
				}, function(error) {
					bitmap.clear()
					reject(error)
				})
				return
			}
			if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
				var filePath = wx.env.USER_DATA_PATH + '/' + fileName
				wx.getFileSystemManager().writeFile({
					filePath: filePath,
					data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
					encoding: 'base64',
					success: function() {
						resolve(filePath)
					},
					fail: function(error) {
						reject(error)
					}
				})
				return
			}
			reject(new Error('not support'))
		})
	}
/**
 * 上传资源到服务器
 * @param   {String} filePath 要上传文件资源的路径
 * @param   {String} path     接口地址
 * @return  {Promise}
 */
function uploadFile(path, filePath, time) {
	console.log("1111",`${API}/${path}`)
	
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `${API}${path}`,
			header: {
				contentType: "multipart/form-data"
			},
			filePath: filePath,
			formData: {
				openId: uni.getStorageSync("openid"),
				timelong: time
			},
			name: 'file',
			success: function(res) {
				uni.hideLoading();
				if (res.statusCode == 403) {
					that.Tips({
						title: res.data
					});
				} else {
					let data = res.data ? JSON.parse(res.data) : {};
					if (data.code == 200) {
						resolve(data);
					} else {
						that.Tips({
							title: data.message
						});
					}
				}
			},
			fail: function(res) {
				uni.hideLoading();
				that.Tips({
					title: res
				});
			}
		})
	})
}


// uploadFile(urlPath, localPath, opt, message) {
// 		let that = this;
// 		return new Promise(async (resolve) => {
// 			//启动上传等待中...
// 			if (message) uni.showLoading({
// 				title: message,
// 			});
// 			uni.uploadFile({
// 				url: urlPath,
// 				filePath: localPath,
// 				name: opt.name || 'pics',
// 				header: {
// 					// #ifdef MP
// 					"Content-Type": "multipart/form-data",
// 					// #endif
// 					[TOKENNAME]: store.state.app.token
// 				},
// 				success: function(res) {
// 					uni.hideLoading();
// 					if (res.statusCode == 403) {
// 						that.Tips({
// 							title: res.data
// 						});
// 					} else {
// 						let data = res.data ? JSON.parse(res.data) : {};
// 						if (data.code == 200) {
// 							resolve(data.data.url);
// 						} else {
// 							that.Tips({
// 								title: data.message
// 							});
// 						}
// 					}
// 				},
// 				fail: function(res) {
// 					uni.hideLoading();
// 					that.Tips({
// 						title: res
// 					});
// 				}
// 			})
// 		})
// 	},

/**
 * 上传多张图片到服务器
 * @param   {Object}  data
 * @return  {Promise}
 */
function uploadimg(options) {
	let url = options.url ? `${API}${options.url}` : `${API}` + 'common/upload';
	uni.chooseImage({
		count: options.length ? options.length : 1,
		sizeType: ['original', 'compressed'],
		sourceType: options.type ? options.type : ['album', 'camera'],
		success: function(res) {
			if (res.errMsg == "chooseImage:ok") {
				if (options.hideloading) {} else {
					uni.showLoading({
						title: options.loadingText ? options.loadingText : '上传中',
						mask: true
					})
				}



				let files = res.tempFiles;
				files.forEach((ele, index) => {
					uni.uploadFile({
						url: url,
						filePath: ele.path,
						name: 'file',
						header: {
							'Authorization': uni.getStorageSync("token") ? uni.getStorageSync("token") : ''
						},
						formData: {
							token: wx.getStorageSync("token"),
						},
						success: function(res) {
							if (files.length - 1 <= index) {
								uni.hideLoading();
							}
							if (options.success) {
								options.success(JSON.parse(res.data).data);
							}
						},
						fali: function(res) {
							uni.hideLoading();
							uni.showToast({
								title: res.description,
								icon: 'none',
								duration: 2000
							});
							if (options.error) {
								options.error(JSON.parse(res.data).data);
							}
						}
					});
				});

			} else {
				uni.showToast({
					title: res.errMsg,
					icon: 'none',
					duration: 2000
				})
			}
		}
	})
}



function uploadimgfilepath(options) {
	             let url = options.url ? `${API}${options.url}` : `${API}` + 'common/upload';

	console.log(options.filePath)
					uni.uploadFile({
						url: url,
						filePath: options.filePath,
						name: 'file',
						header: {
							'Authorization': uni.getStorageSync("token") ? uni.getStorageSync("token") : ''
						},
						formData: {
							token: wx.getStorageSync("token"),
						},
						success: function(res) {
							// if (files.length - 1 <= index) {
							// 	uni.hideLoading();
							// }
							if (options.success) {
								options.success(JSON.parse(res.data).data);
							}
						},
						fali: function(res) {
							// uni.hideLoading();
							// uni.showToast({
							// 	title: res.description,
							// 	icon: 'none',
							// 	duration: 2000
							// });
							if (options.error) {
								options.error(JSON.parse(res.data).data);
							}
						}
					});
				// });


				// uni.showToast({
				// 	title: res.errMsg,
				// 	icon: 'none',
				// 	duration: 2000
				// })



}


function uploadvideo(options) {

	let url = options.url ? `${API}${options.url}` : `${API}` + 'common/uploadVideo';
	uni.chooseVideo({
		sourceType: options.type ? options.type : ['album', 'camera'],
		maxDuration: options.duration ? options.duration : 60,
		camera: 'back',
		success(res) {
			if (res.errMsg == 'chooseVideo:ok') {
				if (options.hideloading) {} else {
					uni.showLoading({
						title: options.loadingText ? options.loadingText : '上传中',
						mask: true
					})
				}
				uni.uploadFile({
					url: url,
					filePath: res.tempFilePath,
					name: 'file',
					formData: {
						openId: uni.getStorageSync("openid"),
					},
					success(suc) {
						uni.hideLoading();
						if (options.success) {
							options.success(JSON.parse(suc.data).data);
						}
					},
					fail(res) {
						uni.hideLoading();
						uni.showToast({
							title: res.description,
							icon: 'none',
							duration: 2000
						});
						if (options.error) {
							options.error(JSON.parse(res.data).data);
						}

					}
				})
			} else {
				uni.showToast({
					title: res.errMsg,
					icon: 'none',
					duration: 2000
				})
			}
		},
		fail(res) {
			console.log(res);
		}
	})
}


function uploadvideoNew(opt, successCallback, errorCallback) {
		// console.log(API)
	   let url = opt.url ? `${API}${opt.url}` : `${API}` + 'common/uploadVideo';
		let that = this;
		if (typeof opt === 'string') {
			let url = opt;
			opt = {};
			opt.url = url;
		}
		let count = opt.count || 1,
			sizeType = opt.sizeType || ['compressed'],
			sourceType = opt.sourceType || ['album', 'camera'],
			is_load = opt.is_load || true,
			uploadUrl = opt.url || '',
			inputName = opt.name || 'file',
			pid = opt.pid,
			fils = '',
			uploadMaxSize = 3,
			// urlPath =API + '/api/front/upload/file' + "?model=" + opt.model +
			// 	"&pid=" + opt.pid,
			urlPath =	'common/uploadVideo',
			model = opt.model;

		uni.chooseVideo({
			sourceType: sourceType,
			success: async (res) => {

				console.log("",res)
				if (Math.ceil(res.size / 1024) < uploadMaxSize * 1024) {
					let  r= await that.uploadFile(urlPath, res.tempFilePath, opt, '视频上传中');
					//获取视频第一帧为封面图，只兼容h5
						console.log("r",r)
					fils =  r.data.videoUrl
										console.log("successsuccesssuccess222222",fils)
										
					// #ifdef H5
					let dataURL = '';
					//uni.createVideoContext('1', that).play()
					let video = document.createElement("video");
					video.setAttribute('crossOrigin', 'anonymous'); //处理跨域
					video.setAttribute('src', fils);
					video.setAttribute('width', 400);
					video.setAttribute('height', 240);
					video.setAttribute('preload', 'auto');
								console.log("successsuccesssuccess2222221111")
					video.addEventListener('loadeddata', async function() {
						uni.showLoading({
							title: '加载中...',
						});
									console.log("successsuccesssuccess222222")
						let canvas = document.createElement("canvas"),
							width = video.width, //canvas的尺寸和图片一样
							height = video.height;
						canvas.width = width;
						canvas.height = height;
						canvas.getContext("2d").drawImage(video, 0, 0, width,
							height); //绘制canvas
						dataURL = canvas.toDataURL('image/png'); //转换为base64
						let file = await that.base64ToFile(dataURL); //base64转文件流
						
		
							let url = 'common/upload';
						// let urlPaths = API+ '/api/front/upload/image' +
						// 	"?model=product&pid=1"; //图片上传地址
						let  rcover = await that.uploadFile(url, file, opt) //封面图
	
				let cover =  rcover.data.imgUrl;
		
						successCallback && successCallback({
							fils: fils,
							cover: cover
						})
					});
					// #endif
					// #ifndef H5
					successCallback && successCallback({
						fils: fils,
						coverURL: ''
					})
					// #endif

				} else {
					// #ifdef APP-PLUS
					plus.nativeUI.alert(`您选择的视频超出${uploadMaxSize}MB,已过滤`);
					// #endif
					// #ifndef APP-PLUS
					uni.showModal({
						content: `您选择的视频超出${uploadMaxSize}MB,已过滤`,
						showCancel: false
					});
					// #endif
				}

			}
		})
	}


/**
	 * base64格式转为文件流
	 * @param base64 base64地址
	 * @returns {File}
	 */
	function base64ToFile(base64) {
		return new Promise(function(resolve, reject) {
			if (typeof window === 'object' && 'document' in window) {
				base64 = base64.split(',')
				var type = base64[0].match(/:(.*?);/)[1]
				var str = atob(base64[1])
				var n = str.length
				var array = new Uint8Array(n)
				while (n--) {
					array[n] = str.charCodeAt(n)
				}
				return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], {
					type: type
				})))
			}
			var extName = base64.match(/data\:\S+\/(\S+);/)
			if (extName) {
				extName = extName[1]
			} else {
				reject(new Error('base64 error'))
			}
			var fileName = Date.now() + '.' + extName
			if (typeof plus === 'object') {
				var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
				bitmap.loadBase64Data(base64, function() {
					var filePath = '_doc/uniapp_temp/' + fileName
					bitmap.save(filePath, {}, function() {
						bitmap.clear()
						resolve(filePath)
					}, function(error) {
						bitmap.clear()
						reject(error)
					})
				}, function(error) {
					bitmap.clear()
					reject(error)
				})
				return
			}
			if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
				var filePath = wx.env.USER_DATA_PATH + '/' + fileName
				wx.getFileSystemManager().writeFile({
					filePath: filePath,
					data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
					encoding: 'base64',
					success: function() {
						resolve(filePath)
					},
					fail: function(error) {
						reject(error)
					}
				})
				return
			}
			reject(new Error('not support'))
		})
	}
/**
	 * opt  object | string
	 * to_url object | string
	 * 例:
	 * this.Tips('/pages/test/test'); 跳转不提示
	 * this.Tips({title:'提示'},'/pages/test/test'); 提示并跳转
	 * this.Tips({title:'提示'},{tab:1,url:'/pages/home/index'}); 提示并跳转值table上
	 * tab=1 一定时间后跳转至 table上
	 * tab=2 一定时间后跳转至非 table上
	 * tab=3 一定时间后返回上页面
	 * tab=4 关闭所有页面跳转至非table上
	 * tab=5 关闭当前页面跳转至table上
	 */
function Tips(opt, to_url) {
		if (typeof opt == 'string') {
			to_url = opt;
			opt = {};
		}
		let title = opt.title || '',
			icon = opt.icon || 'none',
			endtime = opt.endtime || 2000,
			success = opt.success;
		if (title) uni.showToast({
			title: title,
			icon: icon,
			duration: endtime,
			success
		})
		if (to_url != undefined) {
			if (typeof to_url == 'object') {
				let tab = to_url.tab || 1,
					url = to_url.url || '';
				switch (tab) {
					case 1:
						//一定时间后跳转至 table
						setTimeout(function() {
							uni.switchTab({
								url: url
							})
						}, endtime);
						break;
					case 2:
						//跳转至非table页面
						setTimeout(function() {
							uni.navigateTo({
								url: url,
							})
						}, endtime);
						break;
					case 3:
						//返回上页面
						setTimeout(function() {
							// #ifndef H5
							uni.navigateBack({
								delta: parseInt(url),
							})
							// #endif
							// #ifdef H5
							history.back();
							// #endif
						}, endtime);
						break;
					case 4:
						//关闭当前所有页面跳转至非table页面
						setTimeout(function() {
							uni.reLaunch({
								url: url,
							})
						}, endtime);
						break;
					case 5:
						//关闭当前页面跳转至非table页面
						setTimeout(function() {
							uni.redirectTo({
								url: url,
							})
						}, endtime);
						break;
				}

			} else if (typeof to_url == 'function') {
				setTimeout(function() {
					to_url && to_url();
				}, endtime);
			} else {
				//没有提示时跳转不延迟
				setTimeout(function() {
					uni.navigateTo({
						url: to_url,
					})
				}, title ? endtime : 0);
			}
		}
	};
function go(url, type = 1, time) {
	
	console.log("gogogogogogo",url)




	// navTo

	// uni.navigateTo({
	// 	url: url,
	// })
	//type 1:navigate,2redirectTo,3reLaunch
	//list:判断是否需要登录页面数组
	let list = authList;
	// '/pages/chat/index',
	let tabbarList = [
	'/pages/home/index',
	'/pages/profile/profile',
	'/pages/home/ys',
	'/pages/home/service',
	'/pages/home/find'];
	let pageList = [
		'/pages/cart/index'
	];
	throttle(function() {
		// // 判断是否需要登录再跳转

		// for (var i = 0; i < list.length; i++) {
		// 	if (url.indexOf(list[i]) >= 0 && !uni.getStorageSync("token")) {
		// 		uni.navigateTo({
		// 			url: '/pages/user/passport/login?url='+url,
		// 		})
		// 		return
		// 	}
		// }""
		if(type != 3) {
			// 判断是否需要登录再跳转
			for (var i = 0; i < tabbarList.length; i++) {
				if (url.indexOf(tabbarList[i]) >= 0) {
					uni.switchTab({
						url: url
					})
					return
				} else if (url.indexOf(pageList[i]) >= 0) {
					uni.navigateTo({
						url: url
					})
					return
				}
			}
		}

		let pages = getCurrentPages();
		if (time) {
			setTimeout(() => {
				if (type == 1) {

					if (pages && pages.length >= 10) {
						uni.reLaunch({
							url: url,
						})
					} else {
						uni.navigateTo({
							url: url,
						})
					}
				} else if (type == 2) {
					uni.redirectTo({
						url: url,
					})
				} else if (type == 3) {
					uni.reLaunch({
						url: url,
					})
				}
			}, time);
		} else {
			if (type == 1) {
				if (pages && pages.length >= 10) {
					uni.reLaunch({
						url: url,
					})
				} else {
						// console.log('22222222222222')
					uni.navigateTo({
						url: url,
					})
				}
			} else if (type == 2) {
				uni.redirectTo({
					url: url,
				})
			} else if (type == 3) {
				uni.reLaunch({
					url: url,
				})
			}
		}
	}, 500);

};

function back(type, time) {
	let pages = getCurrentPages();
	if (pages.length <= 1) {
		go('/pages/home/index',3)
		return
	}
	if (time) {
		setTimeout(() => {
			uni.navigateBack({
				delta: type ? type : 1
			})
		}, time);
	} else {
		uni.navigateBack({
			delta: type ? type : 1
		})
	}
};

function $toast(tip, icon, time) {
	// #ifdef APP-PLUS
	plus.nativeUI.setUIStyle('light');
	// #endif
	uni.showToast({
		title: tip,
		icon: icon ? icon : 'none',
		duration: time ? time : 2000
	})
}
/**
 *腾讯地图转百度地图经纬度
 */
function qqMapTransBMap(lng, lat) {
	let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
	let x = lng;
	let y = lat;
	let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
	let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
	let lngs = z * Math.cos(theta) + 0.0065;
	let lats = z * Math.sin(theta) + 0.006;
	return {
		lng: lngs,
		lat: lats
	};
}
let throttle_lastTime;

function throttle(fn, gapTime) {
	console.log('gapTime:'+gapTime)
	if (gapTime == null || gapTime == undefined) {
		gapTime = 1500
	}
	let _nowTime = parseInt(((new Date()).getTime()));
	console.log('_nowTime:'+_nowTime)
	if (_nowTime - throttle_lastTime > gapTime || !throttle_lastTime) {
		fn.apply(this, arguments) //将this和参数传给原函数
		throttle_lastTime = _nowTime
	}
}
/*函数防抖*/
function debounce(fn, interval) {
	var timer;
	var gapTime = interval || 1000; //间隔时间，如果interval不传，则默认1000ms
	clearTimeout(timer);
	var context = this;
	var args = arguments; //保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
	timer = setTimeout(function() {
		fn.call(context, args);
	}, gapTime);
}
function shareWX(options) {
	uni.share({
		provider: 'weixin',
		type: 5,
		title: options.title ? options.title : '',
		// href: options.url ? options.url : '',
		imageUrl: options.icon,
		summary: options.text ? options.text : '',
		scene: options.type == 1 ? 'WXSceneSession' : 'WXSenceTimeline',
		miniProgram: {
			id: '',
			path: options.url ? options.url : '',
			type: 0, // 0-正式版； 1-测试版； 2-体验版。
			webUrl: '',
		},
		success() {
			if (options.success) {
				options.success();
			}
		},
		fail(res) {
			if (options.fail) {
				options.fail();
			}
		}
	})
}

function saveVideo(url) {
	uni.showLoading({
		title: '下载中',
		mask: true
	});
	// #ifdef H5
	//**调用download.js下载，下载速度相对快一些**
					let downjs = require("./videodown.js"); //引用当前目录下的自定义函数
					let x=new XMLHttpRequest();
					x.open("GET", url, true);
					x.responseType = 'blob';
					x.onload=function(e) {
						downjs(x.response, "9898.mp4", "video/mp4" )
						uni.hideLoading();
						$toast('下载完成');
					};
					x.send();
	// #endif
	// #ifndef H5

	// #endif
	uni.downloadFile({
		url: url, //仅为示例，并非真实的资源
		success: (res) => {
			if (res.statusCode === 200) {
				uni.saveVideoToPhotosAlbum({
					filePath: res.tempFilePath,
					success: function() {
						uni.hideLoading();
						$toast('下载完成');
					}
				})
			}
		},
		complete: (res) => {
			uni.hideLoading();
			// console.log(res);
		}
	});
}

function saveImg(arr) {
	uni.showLoading({
		title: '下载中',
		mask: true
	});
	arr.forEach((ele, index) => {
		uni.downloadFile({
			url: ele, //仅为示例，并非真实的资源
			success: (res) => {
				console.log(res, index);
				if (res.statusCode === 200) {
					uni.saveImageToPhotosAlbum({
						filePath: res.tempFilePath
					})
				}

				if (index >= arr.length - 1) {
					uni.hideLoading();
					$toast('下载完成');
				}
			},
			complete: (res) => {
				// console.log(res);
			}
		});
	})
}

function timeChange(source, inFormat, outFormat) {
	var checkTime = function(time) {
		if (time < 10) {
			time = "0" + time;
		};
		return time;
	};
	switch (inFormat) {
		case 'Y-m-d H:i:s':
			var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
			source = source.match(reg);
			source = new Date(source[1], source[3] - 1, source[4], source[5], source[6], source[7]);
			break;
		case 'Y-m-d H:i':
			var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2})$/;
			source = source.match(reg);
			source = new Date(source[1], source[3] - 1, source[4], source[5], source[6]);
			break;
		case 'Y-m-d':
			var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
			source = source.match(reg);
			source = new Date(source[1], source[3] - 1, source[4]);
			break;
		case 'timestamp':
			source = new Date(parseInt(source));
			break;
	};
	switch (outFormat) {
		case 'Y-m-d H:i:s':
			return source.getFullYear() + '-' +
				checkTime(source.getMonth() + 1) +
				'-' +
				checkTime(source.getDate()) +
				' ' +
				checkTime(source.getHours()) +
				':' +
				checkTime(source.getMinutes()) +
				':' +
				checkTime(source.getSeconds());
			break;
		case 'Y-m-d H:i':
			return source.getFullYear() + '-' +
				checkTime(source.getMonth() + 1) +
				'-' +
				checkTime(source.getDate()) +
				' ' +
				checkTime(source.getHours()) +
				':' +
				checkTime(source.getMinutes());
			break;
		case 'Y-m-d':
			return source.getFullYear() + '-' +
				checkTime(source.getMonth() + 1) +
				'-' +
				checkTime(source.getDate());
			break;
		case 'Y.m.d':
			return source.getFullYear() + '.' +
				checkTime(source.getMonth() + 1) +
				'.' +
				checkTime(source.getDate());
			break;
		case 'm-d H:i':
			return checkTime(source.getMonth() + 1) +
				'-' +
				checkTime(source.getDate()) +
				' ' +
				checkTime(source.getHours()) +
				':' +
				checkTime(source.getMinutes());
			break;
		case 'm月d日 H:i':
			return checkTime(source.getMonth() + 1) +
				'月' +
				checkTime(source.getDate()) +
				'日 ' +
				checkTime(source.getHours()) +
				':' +
				checkTime(source.getMinutes());
			break;
		case 'm月d日':
			return checkTime(source.getMonth() + 1) +
				'月' +
				checkTime(source.getDate()) +
				'日';
			break;
		case 'm-d':
			return checkTime(source.getMonth() + 1) +
				'-' +
				checkTime(source.getDate());
			break;
		case 'm.d':
			return checkTime(source.getMonth() + 1) +
				'.' +
				checkTime(source.getDate());
			break;
		case 'm.d H:i':
			return checkTime(source.getMonth() + 1) +
				'.' +
				checkTime(source.getDate()) +
				' ' +
				checkTime(source.getHours()) +
				':' +
				checkTime(source.getMinutes());
			break;
		case 'H:i':
			return checkTime(source.getHours()) +
				':' +
				checkTime(source.getMinutes());
			break;
		case 'timestamp':
			return parseInt(source.getTime() / 1000);
			break;
		case 'newDate':
			return source;
			break;
		case 'Y/m/d':
			return source.getFullYear() + '/' +
				checkTime(source.getMonth() + 1) +
				'/' +
				checkTime(source.getDate());
			break;
	};
}

function hideText(text) {
	if(!text){return};
	let  questionArr = text.split('');
	let questionTip = '';
	questionArr.forEach((ele,index) => {
		if(index%2 == 1) {
			questionTip += '*';
		}else{
			questionTip += ele;
		}
	});
	return questionTip
}
function uniCopy({content,success,error}) {
	if(!content) return error('复制的内容不能为空 !')
	content = typeof content === 'string' ? content : content.toString() // 复制内容，必须字符串，数字需要转换为字符串
	/**
	 * 小程序端 和 app端的复制逻辑
	 */
	//#ifndef H5
	uni.setClipboardData({
		data: content,
		success: function() {
			success("复制成功~")
			console.log('success');
		},
		fail:function(){
			success("复制失败~")
		}
	});
	//#endif

	/**
	 * H5端的复制逻辑
	 */
	// #ifdef H5
	if (!document.queryCommandSupported('copy')) { //为了兼容有些浏览器 queryCommandSupported 的判断
		// 不支持
		error('浏览器不支持')
	}
	let textarea = document.createElement("textarea")
	textarea.value = content
	textarea.readOnly = "readOnly"
	document.body.appendChild(textarea)
	textarea.select() // 选择对象
	textarea.setSelectionRange(0, content.length) //核心
	let result = document.execCommand("copy") // 执行浏览器复制命令
	if(result){
		success("复制成功~")
	}else{
		error("复制失败，请检查h5中调用该方法的方式，是不是用户点击的方式调用的，如果不是请改为用户点击的方式触发该方法，因为h5中安全性，不能js直接调用！")
	}
	textarea.remove()
	// #endif
}
function setCopy() {
	 return new Promise((success, fail) => {
	    // #ifdef H5
	    try {
	      navigator.clipboard.readText().then(success).catch(fail)
	    } catch (error) {
	      fail(error)
		  $toast('粘贴失败');
	    }
	    // #endif
	  })
}
function setTitle(name) {
	uni.setNavigationBarTitle({
		title: name ? name : getApp().globalData.name
	})
}
function showModal (options,self) {
	let i18n = self.$t('model');
	uni.showModal({
		title: options.title ? options.title : '',
		// content: options.content ? i18n[options.content] : '',
		content: options.content ? options.content : '',
		showCancel: options.showCancel ? options.showCancel : true,
		cancelText:  options.cancelText ? options.cancelText : i18n['取消'],
		confirmText: options.confirmText ? options.confirmText  : i18n['确认'],
		cancelColor:  options.cancelColor ? options.cancelColor : '#383838',
		confirmColor: options.cancelColor ? options.cancelColor : '#78CA73',
		success:function(res){
			if(res.confirm) {
				if(options.success) {
					options.success();
				}
			}else{
				if(options.cancel) {
					options.cancel();
				}

			}
		}
	})
}
function wxAuthH5(id) {
		let appid = 'wx5d00782d2a97b460';
		let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' +
			encodeURIComponent(window.location.href) +
			'&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect';

		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]);
			return null;
		};

		function postCode(code) {
			ajax({
				url: 'common/gzh/getAccessToken',
				data: {
					code: code
				},
				success(res) {
					if (res.data.errcode == 40029) { //重新授权
						uni.hideLoading();
						$toast('授权失败');
						return;
					}
					ajax({
						url: 'common/gzh/getUser',
						data: {
							access_token: res.data.access_token,
							openid: res.data.openid,
							inviteCode: id ? id : '',
						},
						method: 'post',
						success(info) {
							uni.setStorageSync('token', info.data.tokenHead + ' ' + info.data.token);
							var url = window.location.href; //页面url
							var beforeUrl = url.substr(0, url.indexOf("?"));
							var afterUrl = url.substr(url.indexOf("#"));
							window.location.href = beforeUrl + afterUrl;
							uni.hideLoading();
						}

					})
				}
			})
		};
		getUserInfo();

		function getUserInfo() {

			if (getQueryString('code') != null) {
				uni.showLoading({
					title: '授权中...'
				})
				postCode(getQueryString('code'));
			} else {
				window.location.href = url;
			}
		};
	};
	function wxPayH5(options) {

		//调用微信JS api 支付
		function jsApiCall() {
			WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				JSON.parse(options.data),
				function(res) {
					//根据不同的操作结果进行不同的响应
					switch (res.err_msg) {
						case 'get_brand_wcpay_request:cancel':
							$toast('您已取消微信支付');
							if (options.cancel) {
								options.cancel(res);
							}
							break;
						case "get_brand_wcpay_request:ok":
							$toast('微信支付成功');
							if (options.success) {
								options.success(res);
							}
							break;
						default:
							$toast(res.errMsg);
							if (options.error) {
								options.error(res);
							}
							break;
					}
				}
			);

		};
		if (typeof WeixinJSBridge == "undefined") {
			if (document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			} else if (document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', jsApiCall);
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			}
		} else {
			jsApiCall();
		}
	}
module.exports = {
	Tips,
	uploadFile,
	base64ToFile,
	ajax,
	uploadimg,
	uploadimgfilepath,
	uploadvideo,
	uploadvideoNew,
	getToken,
	API,
	go,
	$toast,
	// wxPay,
	qqMapTransBMap,
	throttle,
	debounce,
	back,
	shareWX,
	saveImg,
	timeChange,
	hideText,
	saveVideo,
	uniCopy,
	setCopy,
	setTitle,
	showModal,
	wxAuthH5,
	wxPayH5
}
